/********************************************************************************************
** Copyright � 2011 Nokia Corporation. All rights reserved.
** Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
** Java and all Java-based marks are trademarks or registered trademarks of
** Sun Microsystems, Inc. Other product and company names mentioned herein may be
** trademarks or trade names of their respective owners.
**
** Subject to the conditions below, you may, without charge:
**
** �  Use, copy, modify and/or merge copies of this software and
**    associated content and documentation files (the �Software�)
**
** �  Publish, distribute, sub-licence and/or sell new software
**    derived from or incorporating the Software.
**
** Some of the documentation, content and/or software maybe licensed under open source
** software or other licenses.  To the extent such documentation, content and/or
** software are included, licenses and/or other terms and conditions shall apply
** in addition and/or instead of this notice. The exact terms of the licenses, disclaimers,
** acknowledgements and notices are reproduced in the materials provided.
**
** This file, unmodified, shall be included with all copies or substantial portions
** of the Software that are distributed in source code form.
**
** The Software cannot constitute the primary value of any new software derived
** from or incorporating the Software.
**
** Any person dealing with the Software shall not misrepresent the source of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
********************************************************************************************/
import Qt 4.7

import "DCDataClasses.js" as DCDATA


Rectangle {
    id: jointripdlg
    width: 270
    height: 100
    //opacity:  0.40
    gradient: Gradient {
        GradientStop {
            position: 0.44
            color: "#abd1ee"
        }

        GradientStop {
            position: 1
            color: "#000000"
        }
    }
    z: 60

    property string datacloudurl: ""
    property string username: ""
    property string password: ""
    property string karpoid: ""

    signal routeselected()
    signal showroute()
    signal showAllroute()
    signal cancel()

    ListModel
    {
        id: loadedroutes

    }


    function getActiveInd()
    {
        return routecombobox.selectedind
    }


    function getRouteKarpoidInd(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).routekarpoid;
    }

    function getRouteOwnerKarpoidInd(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).ownerkarpoid;
    }

    function getRouteFrom()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        var routepoints = JSON.parse(loadedroutes.get(ind).route)
        if (routepoints[0].type == "FROMPOINT")
        {
            return routepoints[0]
        }
        else
        {
            return routepoints[1]
        }

    }

    function getRouteTo()
    {
        var ind = routecombobox.selectedind
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        var routepoints = JSON.parse(loadedroutes.get(ind).route)
        if (routepoints[0].type == "TOPOINT")
        {
            return routepoints[0]
        }
        else
        {
            return routepoints[1]
        }
    }

    function getRouteCount()
    {
        return loadedroutes.count
    }

    function getRouteFromInd(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        var routepoints = JSON.parse(loadedroutes.get(ind).route)
        if (routepoints[0].type == "FROMPOINT")
        {
            return routepoints[0]
        }
        else
        {
            return routepoints[1]
        }
    }

    function getRouteToInd(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        var routepoints = JSON.parse(loadedroutes.get(ind).route)
        if (routepoints[0].type == "TOPOINT")
        {
            return routepoints[0]
        }
        else
        {
            return routepoints[1]
        }
    }


    function getRouteRepeatInd(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).repeat;
    }

    function getRouteNameInd(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).name;
    }

    function getRouteDescriptionInd(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).description;
    }


    function getRouteTimeInd(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).time;
    }

    function getRouteCarpoolkarpoidInd(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return ""
        }

        return loadedroutes.get(ind).carpoolkarpoid;
    }

    function getRouteOwnerName(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return "NA"
        }

        if (loadedroutes.get(ind).showownername != "1")
        {
            return "NA"
        }

        var ownerinfo = JSON.parse(loadedroutes.get(ind).ownerinfo)
        return ownerinfo.firstname + " " + ownerinfo.lastname

    }


    function getRouteOwnerHometown(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return "NA"
        }

        if (loadedroutes.get(ind).showownerhometown != "1")
        {
            return "NA"
        }

        var ownerinfo = JSON.parse(loadedroutes.get(ind).ownerinfo)
        return ownerinfo.hometown

    }

    function getRouteOwnerPhone(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return "NA"
        }

        if (loadedroutes.get(ind).showownerphone != "1")
        {
            return "NA"
        }

        var ownerinfo = JSON.parse(loadedroutes.get(ind).ownerinfo)
        return ownerinfo.phone

    }

    function getRouteOwnerPhonePassPrivacy(ind)
    {
        if (ind < 0 || ind > loadedroutes.count)
        {
            return "NA"
        }

        var ownerinfo = JSON.parse(loadedroutes.get(ind).ownerinfo)
        return ownerinfo.phone
    }

    function getRoutes(fromorto,lat1,lon1,lat2,lon2)
    {
        console.log("getRoutes fromorto: " + fromorto)
        console.log("LEFT : " + lat1 + ", " + lon1)
        console.log("RIGHT: " + lat2 + ", " + lon2)

        jointripdlg.visible = true

        messagebox.text = "Loading routes..."
        messagebox.show("")

        var dcroutelist = new DCDATA.DCRouteList()
        dcroutelist.karpoid = karpoid
        dcroutelist.password = password
        dcroutelist.fromorto = fromorto
        dcroutelist.lat1 = lat1
        dcroutelist.lon1 = lon1
        dcroutelist.lat2 = lat2
        dcroutelist.lon2 = lon2

        DCDATA.send(dcroutelist, datacloudurl, "getroutesinrect", getroutescb)
    }

    function getroutescb(result)
    {
        console.log("getroutescb:\r\n" + result.statusString)

        loadedroutes.clear()

        if (!result.isError)
        {
            var routearray = []
            for (var i = 0; i < result.routelist.length; i++)
            {
                var routeitem = result.routelist[i]
                routeitem.route = JSON.stringify(routeitem.route)
                routeitem.ownerinfo = JSON.stringify(routeitem.ownerinfo)
                loadedroutes.append(routeitem)

                routearray[i] = routeitem.name

            }

            routecombobox.setComboItems(routearray)
            if (loadedroutes.count > 0)
            {
                routecombobox.setSelectedItem(routearray[0])
                routecombobox.selectedind = 0
            }

            messagebox.visible = false
        }
        else
        {
            messagebox.text = "Failed: " + result.errorString
        }

    }


    MessageBox
    {
        id: messagebox
    }



    Rectangle
    {
        id: titlerect
        width: 110
        height: 29
        color: "#5d8eac"
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0

        Text {
            id: titleid
            width: 110
            height: 29
            text: "Routes"
            font.family: "Arial"
            anchors.leftMargin: 10
            anchors.fill: parent
            anchors.verticalCenter: titlerect.verticalCenter
            font.pixelSize: 16
        }

        MouseArea {
            id: titlerectmousearea
            anchors.fill: parent

            drag.target: jointripdlg
            drag.axis: Drag.XandYAxis
            drag.minimumX: 0
            drag.maximumX: 1000
            drag.minimumY: 0
            drag.maximumY: 1000
        }
    }

    ComboBox {
        id: routecombobox
        deletebutton: false
        height: 26
        anchors.top:  titlerect.bottom
        anchors.topMargin: 5
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 10


        onItemSelected:
        {
            //infoid.text = loadedroutes.get(routecombobox.selectedind).description
        }


    }


    PushButton
    {
        id: selectid
        label: "Select"
        width: 60
        anchors.right: parent.right
        anchors.rightMargin: 5
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10


        onButtonpressed:
        {
            if (routecombobox.selectedind < 0)
            {
                messagebox.text = "No route selected."
                messagebox.show()
            }
            else
            {
                jointripdlg.routeselected()

            }
        }

    }

    PushButton
    {
        id: cancelid
        label: "Cancel"
        width: 60
        anchors.right: selectid.left
        anchors.rightMargin: 5
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10

        onButtonpressed:
        {
           jointripdlg.cancel()
        }

    }

    PushButton
    {
        id: showrouteid
        label: "Show"
        width: 60
        anchors.right: cancelid.left
        anchors.rightMargin: 5
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10


        onButtonpressed:
        {
            if (routecombobox.selectedind < 0)
            {
                messagebox.text = "No route selected."
                messagebox.show()
            }
            else
            {
                // show...
                jointripdlg.showroute()
            }
        }

    }

    PushButton
    {
        id: showallrouteid
        label: "Show all"
        width: 60
        anchors.right: showrouteid.left
        anchors.rightMargin: 5
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10


        onButtonpressed:
        {
            if (routecombobox.selectedind < 0)
            {
                messagebox.text = "No route selected."
                messagebox.show()
            }
            else
            {
                // show all...
                jointripdlg.showAllroute()
            }
        }

    }







}
